<?php

namespace Addons\KeFu\Controller;
use Home\Controller\AddonsController;
class KeFuController extends AddonsController
{
    public function keFuLogin()
    {
        $config = get_addon_config('KeFu');

        $error_jump = '<a href="http://' . $config['domain'] . '.kf5.com/user/access">点此普通登录</a>';

        $key = $config['app_key'];
        if(empty($key) || empty($config['domain'])){
            $this->error('参数错误，请联系管理员到插件中心完成配置');
        }

        if (!is_login()) {
            $this->error('请登录该网站'.$error_jump);
        }

        $uid = get_uid();
        $authGroupAccessModel = D('AuthGroupAccess');
        $have_user_group_ids = $authGroupAccessModel->where(array('uid' => $uid))->select();
        $group_ids = getSubByKey($have_user_group_ids, 'group_id');

        if (!array_intersect($group_ids, $config['user_group'])) {
            $this->error('该用户组不允许进行单点登录'.$error_jump);
        }

        $email = UCenterMember()->where(array('id' => $uid))->getField('email');
        if (!$email) {
            $this->error('邮箱未绑定，请绑定后登录'.$error_jump , U('ucenter/config/index'));
        }
        /*
         * Single-sign-on 单点登录验证返回通知　For PHP
         */
        /* 已验证登录的用户信息 */
        $username = $email;

        /* 您的安全校验码(API通信密匙) */

        /* 您的云客服平台地址 */
        $url = "http://" . $config['domain'] . ".kf5.com/user/remote";
        /* 建立通信串 */
        $time = isset($_GET['time']) ? $_GET['time'] : time();
        $msg = $username . $time . $key;
        $token = MD5($msg);

        $url .= "?username=" . $username . "&time=" . $time . "&token=" . $token;

        /* 指定用户名或者手机(可选) */
        //$name = "用户昵称";
        //$url .= "&name=".$name;
        //$phone = "138********";
        //$url .= "&phone=".$phone;


        /* 指定回跳地址(可选) */
        //$return_to = isset($_GET['return_to']) ? $_GET['return_to'] : '';
        //$url .= '&return_to='.$return_to;

        /* 跳转回帮助台验证登录 */
        header("Location: " . $url);
    }

    public function keFulogout(){
        $config = get_addon_config('KeFu');
        $this->success('退出登录成功',"http://" .  $config['domain'] . ".kf5.com/");

    }

}